package com.flowable.controller;

import com.flowable.common.result.CommonResult;
import com.flowable.service.ProcTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 任务 控制器
 */
@RestController
@RequestMapping("/task")
public class ProcTaskController {

    @Autowired
    private ProcTaskService procTaskService;

    /**
     * 启动流程实例
     *
     * @param processDefinitionId 流程定义id
     * @param assignees           下一任务处理人(们)
     * @param extraData           额外信息
     * @return
     */
    @GetMapping("/runProcess")
    public CommonResult runProcess(@RequestParam(value = "processDefinitionId") String processDefinitionId,
                                   @RequestParam(value = "assignees") String[] assignees,
                                   @RequestParam(value = "extraData", required = false) String extraData) {
        return procTaskService.runProcess(processDefinitionId, assignees, extraData);
    }

    /**
     * 查询指定用户的待办任务
     *
     * @param processDefinitionId 流程定义id
     * @param processInstanceId   流程实例id
     * @param taskAssignee        当前任务处理人
     * @return
     */
    @GetMapping("/queryTask")
    public CommonResult queryTodoTask(@RequestParam(value = "processDefinitionId", required = false) String processDefinitionId,
                                      @RequestParam(value = "processInstanceId", required = false) String processInstanceId,
                                      @RequestParam(value = "taskAssignee", required = false) String taskAssignee) {
        return procTaskService.queryTask(processDefinitionId, processInstanceId, taskAssignee);
    }

    /**
     * 完成当前任务，并提交到下一阶段任务
     *
     * @param processDefinitionId 流程定义id
     * @param processInstanceId   流程实例id
     * @param taskAssignee        当前任务处理人
     * @param assignees           下一任务处理人(们)
     * @param extraData           额外信息
     * @return
     */
    @GetMapping("/completeTask")
    public CommonResult completeTask(@RequestParam(value = "processDefinitionId", required = false) String processDefinitionId,
                                     @RequestParam(value = "processInstanceId", required = false) String processInstanceId,
                                     @RequestParam(value = "taskAssignee") String taskAssignee,
                                     @RequestParam(value = "assignees", required = false) String[] assignees,
                                     @RequestParam(value = "extraData", required = false) String extraData) {
        if (processDefinitionId == null && processInstanceId == null) {
            // 如果两个参数都为空，抛出异常或者返回错误信息
            return CommonResult.validateFailed("processDefinitionId and processInstanceId cannot both be null");
        }
        return procTaskService.completeTask(processDefinitionId, processInstanceId, taskAssignee, assignees, extraData);
    }

    /**
     * 回退任务，从源任务到目标任务
     *
     * @param processInstanceId 流程实例id
     * @param sourceTaskKey     源任务taskDefinitionKey
     * @param targetTaskKey     目标任务taskDefinitionKey，为空则返回到上一节点
     * @return
     */
    @GetMapping("/rollbackTask")
    public CommonResult rollbackTask(@RequestParam(value = "processInstanceId") String processInstanceId,
                                     @RequestParam(value = "sourceTaskKey") String sourceTaskKey,
                                     @RequestParam(value = "targetTaskKey", required = false) String targetTaskKey) {
        return procTaskService.rollbackTask(processInstanceId, sourceTaskKey, targetTaskKey);
    }
}
